//
//  TF_LivingTool.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/3/31.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit


class TF_LivingTool: UIView {

    /**宝贝 链接*/
    @objc public let livingaBby = TF_LivingToolBtn()
    /**宝贝 链接总数*/
    @objc public let livingaBbyLabel = UILabel()
    /**聊天栏*/
    @objc public let chatBar = UITextField()
    /**聊天栏*/
    @objc public let chatBarButton = TF_LivingToolBtn()
    /**拉起*/
    @objc public let pull_up = TF_LivingToolBtn()
    /**分享*/
    @objc public let share = TF_LivingToolBtn()
    /**更多详情*/
    @objc public let more = TF_LivingToolBtn()
    /**红 _ 包*/
    @objc public let envelopes = TF_LivingToolBtn()
    /**爱心*/
    @objc public let love = TF_LivingToolBtn()
    /**点赞总数*/
    @objc public let total = UILabel()
    @objc public let buttonArray = NSMutableArray()
    /**是否显示点赞*/
    @objc public var showLive:Bool = false
    /**是否显示宝贝*/
    @objc public var showLivingaBby:Bool = false
    /**是否显示菜单入口:*/
    @objc public var showPull_up:Bool = false
    //【字段含义】邀请分享
    @objc public var inviteRankingShow:Bool = false
    //【字段含义】更多

    
    
   @objc public var moreShow:Bool = false
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.chatBar.textColor = UIColor.white
        self.chatBar.layer.masksToBounds = true
        self.chatBar.layer.cornerRadius = 17.5
        self.chatBar.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.3)
       
    
        self.chatBar.attributedPlaceholder  = NSMutableAttributedString.getAttributed(string: "说点什么", font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 238/255.0, green: 244/255.0, blue: 248/255.0, alpha: 1.0));
        self.addSubview(self.chatBar)
        
        self.addSubview(self.chatBarButton)
        
        
        self.addSubview(self.livingaBby)
        self.buttonArray.add(self.livingaBby)
        self.addSubview(self.pull_up)
        self.buttonArray.add(self.pull_up)
        self.addSubview(self.share)
        self.buttonArray.add(self.share)
        self.addSubview(self.more)
        self.buttonArray.add(self.more)
        self.addSubview(self.envelopes)
        self.buttonArray.add(self.envelopes)
        self.addSubview(self.love)
        self.buttonArray.add(self.love)
        
        self.livingaBby.backgroundColor = UIColor.clear
        self.livingaBby.setImage(UIImage.init(named: "HTLive.bundle/livingaBby"), for: .normal)

        self.livingaBbyLabel.numberOfLines = 0;
        self.livingaBby .addSubview(self.livingaBbyLabel)
        self.livingaBbyLabel.font = UIFont.PingFang(size: 12)
        
        self.livingaBbyLabel.textAlignment = .center
        self.livingaBbyLabel.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        self.pull_up.setImage(UIImage.init(named: "HTLive.bundle/LivingPull_up"), for: .normal)
        self.share.setImage(UIImage.init(named: "HTLive.bundle/LivingShare"), for: .normal)
        self.more.setImage(UIImage.init(named: "HTLive.bundle/LivingMore"), for: .normal)
        self.envelopes.setImage(UIImage.init(named: "HTLive.bundle/LivingEnvelopes"), for: .normal)
       
        self.love.setImage(UIImage.init(named: "HTLive.bundle/LivingFabulous"), for: .normal)
    
        self.total.textAlignment = .center;
        self.total.numberOfLines = 0;
        self.addSubview(self.total)
        self.total.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        
       
        self.total.font = UIFont.PingFang(size: 12)
      
        self.total.layer.masksToBounds  =  true
        self.total.backgroundColor = UIColor(red: 255/255.0, green: 83/255.0, blue: 98/255.0, alpha: 1.0)
       
    }
    @objc public func showAll()
    {

       
        var loveX:CGFloat = 0
        var loveWidth:CGFloat = 35
        if (self.showLive) {
            self.total.isHidden = false
            loveX = self.frame.size.width - 35;
        }else{
            loveWidth = 0
            loveX = self.frame.size.width
            self.total.isHidden = true
        }
        
        self.love.frame = CGRect(x: loveX, y: (self.frame.size.height - loveWidth) / 2, width: loveWidth, height: loveWidth)
        


       var moreX:CGFloat = 0
        if (self.showLive) {
            moreX = loveX - 35 - 10
        }else{
            moreX = loveX - 35
        }
        //更加
        if(self.moreShow){
            self.more.isHidden = false;
            self.more.frame = CGRect(x: moreX, y: (self.frame.size.height - 35) / 2, width: 35, height: 35)
        }else{
            self.more.isHidden = true;
            self.more.frame = self.love.frame;
        }
       
        //邀请分享
        if (self.inviteRankingShow) {
            self.share.isHidden = false;
            self.share.frame = CGRect(x: self.more.frame.origin.x - 35 - 10, y: (self.frame.size.height - 35) / 2, width: 35, height: 35)
        }else{
            self.share.isHidden = true;
            self.share.frame =  self.more.frame;
        }
       
        //菜单入口
        if (self.showPull_up) {
            self.pull_up.isHidden = false
            self.pull_up.frame = CGRect(x: self.share.frame.origin.x - 35 - 10, y: (self.frame.size.height - 35) / 2, width: 35 , height: 35)
        }else{
            self.pull_up.isHidden = true
            self.pull_up.frame = self.share.frame ;
        }
       
        
      var livingaBbyX:CGFloat =  -36.6
        if (self.showLivingaBby) {
            self.livingaBby.isHidden = false
                livingaBbyX = 0;
        }else{
            self.livingaBby.isHidden = true
        }
         //宝_贝链接
         self.livingaBby.frame = CGRect(x: livingaBbyX, y: (self.frame.size.height - 37)/2, width: 36.6, height: 37)
        
        /**宝贝 链接总数*/
        self.livingaBbyLabel.frame = CGRect(x: 0, y: self.livingaBby.frame.size.height - 3 - 16.5, width: self.livingaBby.frame.size.width, height: 16.5)
        
        for view in self.buttonArray {
            let button = view as! UIButton
            button.layer.cornerRadius = button.bounds.size.width / 2;
        }

        let chatBarX:CGFloat = self.livingaBby.frame.maxX + 10
           
        let chatBarW:CGFloat = self.pull_up.frame.origin.x - chatBarX - 10
          
        self.chatBar.frame = CGRect(x: chatBarX, y: (self.frame.size.height - 35) / 2, width: chatBarW, height: 35)

        self.chatBar.leftView = UIView.init(frame: CGRect(x: 0, y: 0, width: 16, height: 44)) 
        self.chatBar.leftViewMode = .always

        self.chatBarButton.frame = self.chatBar.frame;
    }
    
    
    @objc public func praiseAnimation()
    {
        let imageView = UIImageView()
        imageView.frame = CGRect(x: self.love.frame.origin.x + 10, y: self.frame.maxY - self.love.frame.size.height - self.love.frame.origin.y, width: self.love.frame.size.width, height: self.love.frame.size.height)
        
      
        imageView.backgroundColor = UIColor.clear
        imageView.clipsToBounds = true
        self.superview!.addSubview(imageView)
        
        
        let one = CGFloat(arc4random()%200)
        let two = CGFloat(arc4random()%900)
        
        let startX :CGFloat = round(one)

        let speed:CGFloat  = 1 / round(two) + 0.6
        let duration:CGFloat = 7.0 * speed
     
        let aa = CGFloat(arc4random()%12)
        let imageName = Int(aa+1)


        imageView.image = UIImage.init(named: NSString.init(format: "heart%d.png", imageName) as String)
        UIView.animate(withDuration: duration) {
            imageView.frame = CGRect(x: TalkfunSharedTools.getMainScreen().size.width - startX, y: -100, width: 35, height: 35)
        } completion: { aa in
            imageView.removeFromSuperview()
            imageView.isHidden = true
        }


    }
    @objc public func updateTotal(total:NSInteger)
    {
        
        let text:NSString = NSString.init(format:"%ld", total) as NSString
        self.total.text = text as String

    
        let attrsSize: CGSize = text.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 12) as Any])
        
     
        self.total.bounds = CGRect(x: 0, y: 0, width: attrsSize.width + 11, height: 20)
        self.total.layer.cornerRadius = self.total.frame.size.height / 2
        
        self.total.center = self.love.center;
       var totalWidth:CGFloat = attrsSize.width + 11;
        if (totalWidth < 20) {
            totalWidth = 20;
        }
        self.total.frame = CGRect(x: self.total.frame.origin.x, y: -22.5, width: totalWidth, height: 20)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
